package com.minelx.datamicroscope.launch.commandline;

import com.alibaba.fastjson.JSONObject;
import com.minelx.datamicroscope.common.EnrichedIterator;
import com.minelx.datamicroscope.core.query.Query;
import io.gitee.minelx.filesystem.Directory;
import io.gitee.minelx.filesystem.provider.FS;
import io.gitee.minelx.filesystem.File;
import io.gitee.minelx.commontools.Files;
import io.gitee.minelx.commontools.date.Day;

import java.nio.charset.StandardCharsets;
import java.util.List;

import static io.gitee.minelx.filesystem.init.OpenPolicy.CREATE;
import static io.gitee.minelx.filesystem.init.OpenPolicy.EXISTS;
import static java.util.stream.Collectors.toList;

public class AnhuiYinShangQueryMainTest {
	public static void main(String[] args) {
		Directory directory = FS.workingDir().child().dir("订单号", EXISTS);
		Directory destinationDir = FS.workingDir().child().dir("订单号DEST", EXISTS);

		List<Day> days = Day.of(2022, 4, 1)
				.daysBetween(Day.of(2022, 5, 1))
				.collect(toList());
		EnrichedIterator.fromUnSafeIterator(days.iterator())
				.consumeAll(day -> {
					eachDay(directory, destinationDir, day);
				}, 4);
	}

	private static void eachDay(Directory directory, Directory destinationDir, Day day) {
		String description = day.format("yyyyMMdd");
		File file = directory.child().file(description + ".csv", EXISTS);
		String queryContent = Files.toString("/AnhuiYinShangQueryMainTest__query.json", StandardCharsets.UTF_8)
				.replace("$file$", file.path().absolutePath())
				.replace("$date$", description);
		Query.localFSAndOnlineIndexWithPandasCalculationSupport(JSONObject.parseObject(queryContent))
				.queryTo(destinationDir.child().file(description + ".csv", CREATE).path().uri());
	}
}
